package com.msh.starter.qrcode.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;

public class QrCodeUtil {
  private final static int BLACK = 0xFF000000;
  private final static int WHITE = 0xFFFFFFFF;
  private final static MultiFormatWriter multiFormatWriter = new MultiFormatWriter();


  /**
   * 生成带有logo的二维码
   *
   * @param content 二维码内同
   * @param logo logo图片
   */
  public static BufferedImage generateQrCodeWithLogo(String content, BufferedImage logo, int width, int height, BarcodeFormat barcodeFormat, ErrorCorrectionLevel errorCorrectionLevel)
      throws WriterException, IOException {
    BitMatrix bitMatrix = generateQrCode(content, width, height, barcodeFormat, errorCorrectionLevel);
    BufferedImage qrCode = bitMatrixToBufferedImage(bitMatrix);
    qrCode = logoMatrix(qrCode, logo);
    return qrCode;
  }

  /**
   * 生成二维码
   *
   * @param content 二维码内容
   * @param width 宽度
   * @param height 高度
   */
  public static BitMatrix generateQrCode(String content, int width, int height, BarcodeFormat barcodeFormat,ErrorCorrectionLevel errorCorrectionLevel) throws WriterException {
    HashMap map = new HashMap(3);
    map.put(EncodeHintType.CHARACTER_SET, "utf-8");
    map.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
    map.put(EncodeHintType.MARGIN, 0);
    return multiFormatWriter.encode(content, barcodeFormat, width, height, map);
  }

  /**
   * 设置 logo
   *
   * @param matrixImage 源二维码图片
   * @return 返回带有logo的二维码图片
   * @author Administrator sangwenhao
   */
  private static BufferedImage logoMatrix(BufferedImage matrixImage, BufferedImage logo)
      throws IOException {
    /**
     * 读取二维码图片，并构建绘图对象
     */
    Graphics2D g2 = matrixImage.createGraphics();
    int matrixWidth = matrixImage.getWidth();
    int matrixHeight = matrixImage.getHeight();

    //开始绘制图片
    g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeight / 5 * 2, matrixWidth / 5, matrixHeight / 5,
        null);
    BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    g2.setStroke(stroke);
    //指定弧度的圆角矩形
    RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth / 5 * 2,
        matrixHeight / 5 * 2, matrixWidth / 5, matrixHeight / 5, 20, 20);
    g2.setColor(Color.white);
    g2.draw(round);

    //设置logo 有一道灰色边框
    BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    g2.setStroke(stroke2);
    RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth / 5 * 2 + 2,
        matrixHeight / 5 * 2 + 2, matrixWidth / 5 - 4, matrixHeight / 5 - 4, 20, 20);
    g2.setColor(new Color(128, 128, 128));
    g2.draw(round2);

    g2.dispose();
    matrixImage.flush();
    return matrixImage;
  }

  /**
   * BitMatrix转BufferedImage
   */
  private static BufferedImage bitMatrixToBufferedImage(BitMatrix bitMatrix) {
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        image.setRGB(x, y, (bitMatrix.get(x, y) ? BLACK : WHITE));
      }
    }
    return image;
  }
}
